package List;

public class SinglyLinkedList implements IList{
    public class ListNode{
        public int data;
        public ListNode next;

        public ListNode(int data) {
            this.data = data;
        }
    }

    public ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode newnode = new ListNode(data);
        newnode.next = head;
        head = newnode;
    }

    @Override
    public void addLast(int data) {
        ListNode newnode = new ListNode(data);
        //链表为空
        if(head == null){
            head = newnode;
            return;
        }
        //链表不为空，找尾尾插
        ListNode cur = head;
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = newnode;
    }

    private void CheckIndex(int index){
        int len = this.size();
        if(index < 0 || index > len){
            throw new IllegalIndexException("index不合法");
        }
    }

    @Override
    public void addIndex(int index, int data) {
        try {
            CheckIndex(index);
            ListNode newnode = new ListNode(data);

            if(index == 0){
                addFirst(data);
            }
            if(index == size()){
                addLast(data);
            }

            ListNode cur = head;
            while(index - 1 != 0){
                cur = cur.next;
                index--;
            }
            newnode.next = cur.next;
            cur.next = newnode;

        }catch (IllegalIndexException e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null){
            if(cur.data == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null){
            return;
        }

        //解决头节点问题
        if(head.data == key){
            head = head.next;
            return;
        }

        ListNode cur = head;
        while(cur.next.data != key){
            cur = cur.next;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }



    @Override
    public void removeAllKey(int key) {
        if(head == null){
            return;
        }

        ListNode prev = head;
        ListNode cur = head.next;
        while (cur != null){
            if(cur.data == key){
                prev.next = cur.next;
            }else {
                prev = cur;
            }
            cur = cur.next;
        }
        if(head.data == key){
            head = head.next;
        }

    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;

        while(cur != null){
            count++;
            cur = cur.next;
        }


        return count ;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null){
            ListNode ret = cur;
            cur.next = null;
            cur = ret.next;
        }
        head = null;
    }
}
